/**
 * 项目名称：tv   
 * 类名称：TVProcessor.java   
 * 类描述：   
 * 创建人：issac2009@vip.qq.com   
 * 创建时间：Jul 17, 20116:46:44 PM  
 * 修改人：Thinkpad User
 * 修改时间：Jul 17, 20116:46:44 PM   
 * 修改备注：   
 * @version 
 */
package com.tv.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cityoneyes.util.Util;
import com.tv.bean.Program;
import com.tv.data.service.ITVService;
import com.tv.htmlparser.TVJsoupParser;

/**
 * @ClassName: TVProcessor.java
 * @Description: TODO
 * @author issac2009@vip.qq.com
 * @date Jul 17, 20116:46:44 PM
 */
@Controller("tvProcessor")
public class TVProcessor {
	
	@Autowired
	private ITVService itvService;

	public ITVService getItvService() {
		return itvService;
	}

	public void setItvService(ITVService itvService) {
		this.itvService = itvService;
	}


	/*
	 * 读入txt文件，把数据存入数据库
	 */
	@RequestMapping("initdata")
	public void saveData(){
		//get properties
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("init.properties");   
		Properties p = new Properties();   
		try {   
		   p.load(inputStream);   
		} catch (IOException e1) {   
		   e1.printStackTrace();   
		}   
		String path = p.getProperty("website.src.file");
		List<String> filesList = TVJsoupParser.traversal(path);
		String str = "";
		Program program = null;
		
		if (Util.isNotEmpty(filesList)){
			for (String file : filesList){
				BufferedReader br;
				try {
					br = new BufferedReader(new InputStreamReader(
							new FileInputStream(new File(path + file))));
					int i = 1;
					String[] tvStr = file.split("=");
					tvStr = tvStr[1].split("\\.");
					
					while((str = br.readLine()) != null){
						
						program = new Program();
						String[] data = str.split("\\|");
						program.setOrder_seq(i++);
						program.setProgramName(data[1]);
						program.setProgramTime(data[0]);
						program.setProgramDate(Util.getOnlineBusinessDateStr());
						program.setTvId(new Integer(tvStr[0]));
						
						itvService.save(program);
					}
					
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} 
				
			}
		}
		
	}
	
	public static void main(String[] args) {
		String st = "01:15|人气日报||?|";
		String[] tStrings = st.split("\\|");
		for (int i=0; i<tStrings.length; i++){
			System.out.println(tStrings[i]);
		}
	}

}
